<template>
  <div class="distribution-team">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>团队管理</span>
        </div>
      </template>

      <!-- 团队统计 -->
      <el-row :gutter="20" style="margin-bottom: 20px;">
        <el-col :span="8">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-value">{{ teamStats.level1_count }}</div>
              <div class="stat-label">一级成员</div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-value">{{ teamStats.level2_count }}</div>
              <div class="stat-label">二级成员</div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-value">{{ teamStats.total_count }}</div>
              <div class="stat-label">团队总人数</div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 搜索条件 -->
      <el-form :inline="true" class="search-form">
        <el-form-item label="成员姓名">
          <el-input
            v-model="searchForm.name"
            placeholder="请输入成员姓名"
            clearable
            @clear="handleSearch"
          />
        </el-form-item>
        <el-form-item label="层级">
          <el-select v-model="searchForm.level" placeholder="请选择层级" clearable>
            <el-option label="全部" value="" />
            <el-option label="一级" value="1" />
            <el-option label="二级" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 团队成员列表 -->
      <el-table
        :data="tableData"
        border
        stripe
        v-loading="loading"
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column label="成员" min-width="150">
          <template #default="scope">
            <div style="display: flex; align-items: center;">
              <el-avatar
                :size="40"
                :src="VITE_URL + '/storage/' + scope.row.avatar"
                style="margin-right: 10px;"
              />
              <div>
                <div>{{ scope.row.customer_name }}</div>
                <div style="font-size: 12px; color: #999;">{{ scope.row.telephone }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="level" label="层级" width="80">
          <template #default="scope">
            <el-tag v-if="scope.row.level === 1">一级</el-tag>
            <el-tag v-else type="success">二级</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="加入时间" width="180" />
        <el-table-column label="累计贡献" width="120">
          <template #default="scope">
            ¥{{ scope.row.total_contribution || 0 }}
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @current-change="handlePageChange"
        :current-page="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next"
        :total="total"
        style="margin-top: 20px; text-align: right;"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const VITE_URL = (window as any).VITE_URL

// 表格数据
const tableData = ref([])
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(15)
const total = ref(0)

// 团队统计
const teamStats = ref({
  level1_count: 0,
  level2_count: 0,
  total_count: 0
})

// 搜索表单
const searchForm = ref({
  name: '',
  level: ''
})

// 获取团队成员列表
const getTeamList = async () => {
  loading.value = true
  try {
    const params = new URLSearchParams()
    params.append('page', currentPage.value.toString())
    params.append('limit', pageSize.value.toString())
    
    if (searchForm.value.name) {
      params.append('name', searchForm.value.name)
    }
    if (searchForm.value.level !== '') {
      params.append('level', searchForm.value.level)
    }
    
    const url = `${VITE_URL}/admin.distribution/team_list?${params.toString()}`
    const res = await axios.get(url)
    if (res.data.status === 0) {
      tableData.value = res.data.data.list
      total.value = res.data.data.total
    }
  } catch (error) {
    ElMessage.error('获取团队成员列表失败')
  } finally {
    loading.value = false
  }
}

// 获取团队统计
const getTeamStats = async () => {
  try {
    const url = `${VITE_URL}/admin.distribution/team_stats`
    const res = await axios.get(url)
    if (res.data.status === 0) {
      teamStats.value = res.data.data
    }
  } catch (error) {
    ElMessage.error('获取团队统计失败')
  }
}

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  getTeamList()
}

// 重置
const handleReset = () => {
  searchForm.value = {
    name: '',
    level: ''
  }
  currentPage.value = 1
  getTeamList()
}

// 分页变化
const handlePageChange = (page: number) => {
  currentPage.value = page
  getTeamList()
}

onMounted(() => {
  getTeamList()
  getTeamStats()
})
</script>

<style scoped>
.distribution-team {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.stat-card {
  text-align: center;
}

.stat-content {
  padding: 20px 0;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #667eea;
  margin-bottom: 5px;
}

.stat-label {
  color: #999;
}
</style>